package com.operate.api.operate.banner.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.pagehelper.PageInfo;
import com.operate.api.base.*;
import com.operate.api.operate.banner.domain.vo.BannerAddVO;
import com.operate.api.operate.banner.domain.vo.BannerSearchVO;
import com.operate.api.operate.banner.domain.vo.BannerUpdateVO;
import com.operate.api.operate.banner.domain.vo.BannerVO;
import com.operate.api.operate.banner.service.IBannerService;
import com.operate.api.operate.dramas.domain.vo.VideoSearchVO;
import com.operate.api.operate.dramas.domain.vo.VideoVO;
import com.operate.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api( tags = ApiIndex.BannerController)
@RequestMapping(value = "/api/operate/banner")
@RestController
@SaCheckLogin
public class BannerController {


    @Autowired
    private IBannerService service;



    @ApiOperation(value = "查询Banner列表")
    @GetMapping(value = "/list")
    public R<RestPage<List<BannerVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute BannerSearchVO searchVO) {
        List<BannerVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "创建Banner")
    @PostMapping(value = "/add")
    public R create(@ApiParam(value = "添加参数") @Valid @RequestBody BannerAddVO vo, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.add(vo);
        }
        return result;
    }

    @ApiOperation(value = "删除Banner")
    @PostMapping(value = "/delete")
    public R delete( @ApiParam(value = "BannerID") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }

    @ApiOperation(value = "置顶Banner")
    @PostMapping(value = "/topping")
    public R topping( @ApiParam(value = "BannerID") @Valid @RequestBody IDEntity delete) {
        service.topping(delete.getId());
        return new R();
    }


    @ApiOperation(value = "修改Banner")
    @PostMapping(value = "/update")
    public R update(@ApiParam(value = "修改参数") @Valid  @RequestBody BannerUpdateVO updateVO, BindingResult bindingResult) {
        R result= BindingResultUtil.BindingResult(bindingResult);
        if(result.getCode()== Constants.SYS_SUCCESS){
            service.update(updateVO);
        }
        return result;
    }


    @ApiOperation(value = "查询关联视频列表")
    @GetMapping(value = "/video_list")
    public R<RestPage<List<VideoVO>>> video_list(
            @ApiParam(value = "查询参数") @ModelAttribute VideoSearchVO searchVO) {
        List<VideoVO> list = service.getVideoList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }




}
